package com.dm.wallpaper.library.adapters;

import com.dm.wallpaper.library.ResourceTable;
import com.dm.wallpaper.library.database.DataProvider;
import com.dm.wallpaper.library.database.Database;
import com.dm.wallpaper.library.items.Category;
import com.dm.wallpaper.library.utils.Log;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.app.Context;

import java.util.List;

public class FilterProvider extends BaseItemProvider {

    private List<Category> data;
    private LayoutScatter layoutScatter;


    private final boolean mIsMuzei;

    public FilterProvider(Context context, boolean mIsMuzei) {
        this.mIsMuzei = mIsMuzei;
        data = DataProvider.getInstance().getCategorys();
        layoutScatter = LayoutScatter.getInstance(context);
    }

    @Override
    public int getCount() {
        int count = data != null ? data.size() : 0;
        Log.i("CategriesListProvider.getCount([]):32   " + count);
        return count;
    }

    @Override
    public Object getItem(int i) {
        return data != null ? data.get(i) : null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        component = layoutScatter.parse(ResourceTable.Layout_provider_categories_list, null, false);
        if (position % 2 == 0) {
            ShapeElement shapeElement = new ShapeElement();
            shapeElement.setRgbColor(new RgbColor(0x00ff00));
            component.setBackground(shapeElement);
        } else {
            ShapeElement shapeElement = new ShapeElement();
            shapeElement.setRgbColor(new RgbColor(0xff0000));
            component.setBackground(shapeElement);
        }


        ViewHolder viewHolder = new ViewHolder((ComponentContainer) component);
        viewHolder.setDate(data.get(position), new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Log.i("CategriesListProvider.onClick([component]):56");
                notifyDataChanged();
            }
        });


        Log.i("CategriesListProvider.getComponent([position, component, componentContainer]):61   " + (mIsMuzei ? data.get(position).getMuzeiSelected() == 1 : data.get(position).getSelected() == 1) + "    " + position);
        viewHolder.categoriesCheckbox.setChecked(mIsMuzei ? data.get(position).getMuzeiSelected() == 1 : data.get(position).getSelected() == 1);
        viewHolder.categoriesCheckbox.setCheckedStateChangedListener(new AbsButton.CheckedStateChangedListener() {
            @Override
            public void onCheckedChanged(AbsButton absButton, boolean b) {
                Log.i("CategriesListProvider.onCheckedChanged([absButton, b]):64");
                Database.get().selectCategoryForMuzei(((Category) absButton.getTag()).getId(), b);
                notifyDataChanged();
            }
        });
        return component;
    }


    public boolean checkAllSelect() {
        return getCount() == getSelectedCount();
    }

    public boolean selectAllorNot() {
        boolean isAllSelected = checkAllSelect();
        for (Category category : data) {
//            if (mIsMuzei) {
                Database.get().selectCategoryForMuzei(category.getId(), !isAllSelected);
                category.setMuzeiSelected(isAllSelected ? 0 : 1);
//            } else {
//                Database.get().selectCategory(category.getId(), !isAllSelected);
//                category.setSelected(isAllSelected ? 0 : 1);
//            }
        }
        Log.i("CategriesListProvider.selectAllorNot([]):82");
        notifyDataChanged();
        return !isAllSelected;
    }

    @Override
    public void notifyDataChanged() {
        data = DataProvider.getInstance().getCategorys();
        super.notifyDataChanged();
    }

    private int getSelectedCount() {
        int selected = 0;
        for (Category category : data) {
            if (mIsMuzei) {
                if (category.getMuzeiSelected() == 1) {
                    selected += 1;
                }
            } else {
                if (category.getSelected() == 1) {
                    selected += 1;
                }
            }
        }
        return selected;
    }

    private static class ViewHolder {

        private Text categriesContent;
        private Text categriesNumber;
        private Checkbox categoriesCheckbox;
        private Component itemView;

        private ViewHolder(ComponentContainer componentContainer) {
            itemView = componentContainer;
            categriesContent = (Text) componentContainer.findComponentById(ResourceTable.Id_categries_content);
            categriesNumber = (Text) componentContainer.findComponentById(ResourceTable.Id_categries_number);
            categoriesCheckbox = (Checkbox) componentContainer.findComponentById(ResourceTable.Id_categories_checkbox);
        }

        public void setDate(Category category, Component.ClickedListener clickedListener) {
            categoriesCheckbox.setTag(category);
            categriesContent.setText(category.getName());
            categriesNumber.setText(category.getCount() + "");
            itemView.setClickedListener(clickedListener);
            itemView.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    Log.i("ViewHolder.onClick([component]):130");
                    Database.get().selectCategoryForMuzei(category.getId(), category.getMuzeiSelected() == 0);
                    clickedListener.onClick(component);
                }
            });
        }

    }
}
